
const lineSpace = 40

// function drawWave(canvas,waveData)
// {
// 	// 使用 wx.createContext 获取绘图上下文 context
// 	var context = wx.createCanvasContext(canvas.ID)
// 	context.setStrokeStyle('red')
// 	context.setLineWidth(1)
// 	context.stroke()

// 	var startPoint = 0
// 	for (var i = 0; i < waveData.length; i++)
// 	{
// 		if(waveData.length >= 320)
// 		{
// 			// canvas.drawPoints.slice(canvas.replaceIndex % 320, 1, waveData[i])
// 			// canvas.replaceIndex++
// 			canvas.drawPoints.splice(0)
// 		}
// 		else
// 			canvas.drawPoints[canvas.drawPoints.length] = waveData[i]
// 	}
// 	if (canvas.drawPoints.length == 0)
// 	{
// 		context.moveTo(0, waveData[0])
// 		startPoint = 1
// 	}
// 	for (var i = 0;i < canvas.drawPoints.length ;i++)
// 	{
// 		context.lineTo(i, waveData[i])
// 	}
// 	context.stroke()
// 	context.draw()
// 	// canvas.drawCount += waveData.length
// }

/*
function drawWave(canvas, waveData)
{
	// 使用 wx.createContext 获取绘图上下文 context
	var context = wx.createCanvasContext(canvas.ID)
	context.setStrokeStyle('red')
	context.setLineWidth(1)
	context.stroke()
	context.moveTo(0,waveData[0])
	for (var i = 1; i < canvas.replaceIndex; i++)
	{
		context.lineTo(i, waveData[i])
	}

	context.moveTo(canvas.replaceIndex, waveData[canvas.replaceIndex])
	for (var i = canvas.replaceIndex; i < waveData.length; i++)
	 {
		context.lineTo(i, waveData[i])
	}
	context.stroke()
	context.clearRect(canvas.replaceIndex, 0, lineSpace, canvas.height)

	context.draw()
	// context.draw()

}
*/

function initialWaveView(canvasID,width,height)
{
	var waveV = { "ID": canvasID, "width": width, "height": height, "drawPoints": [], "dropCount": 0, "totalCount": 0, "circleCount": 0 }
	return waveV
}

function drawWave(canvas, waveData,color)
{
	console.log(canvas.replaceIndex)
	let width = canvas.width
	let height = canvas.height
	// 使用 wx.createContext 获取绘图上下文 context
	var context = wx.createCanvasContext(canvas.ID)
	context.setStrokeStyle(color)
	context.setLineWidth(1)
 	//未画满一屏的情况下
	if (canvas.drawPoints.length < width)
	{
		context.moveTo(0, waveData[0])
		for (var i = 1; i < waveData.length; i++)
		{
			context.lineTo(i, (waveData[i] * 1.0 / 128.0 )* height)
		}
		canvas.right = true
	}
	// 画满一屏的情况下
	//只画右边
	else if(canvas.replaceIndex <= lineSpace)
	{
		if (canvas.right)
		{
			context.moveTo(canvas.replaceIndex, waveData[canvas.replaceIndex])
			for (var i = canvas.replaceIndex + 1; i < waveData.length; i++)
			 {
				context.lineTo(i, (waveData[i] * 1.0 / 128.0) * height)
			}
		}
		//只画左边
		else
		{
			context.moveTo(0, waveData[canvas.replaceIndex])
			for (var i = canvas.replaceIndex + 1; i < waveData.length; i++)
			 {
				context.lineTo(i, (waveData[i] * 1.0 / 128.0) * height)
			}

		}
	}
	//左右一起画
	else
	{
		//左
		context.moveTo(0, waveData[0])
		for (var i = 1; i < canvas.replaceIndex - lineSpace; i++)
		{
			context.lineTo(i, (waveData[i] * 1.0 / 128.0) * height)
		}
		//右
		context.moveTo(canvas.replaceIndex , waveData[canvas.replaceIndex])
		for (var i = canvas.replaceIndex + 1; i < waveData.length; i++)
		{
			context.lineTo(i, (waveData[i] * 1.0 / 128.0) * height)
		}
		canvas.left = true
		canvas.right = false
	}
	// 只画左边
	// else
	// {
	// // 	//左
	// // 	context.moveTo(canvas.replaceIndex, waveData[canvas.replaceIndex])
	// // 	for (var i = 0; i < canvas.replaceIndex; i++)
	// // 	{
	// // 		context.lineTo(i, waveData[i])
	// // 	}
	// console.log(1)
	// }
	context.stroke()
	context.draw()

}

function addPointsWithGainline(canvas,waveData,color,gain,gainColor)
{
	addPoints(canvas, waveData, color)
	drawGain(canvas, gain, gainColor, context)
}

function addPoints(canvas, waveData, color, gain, gainColor)
{
 	var context = wx.createCanvasContext(canvas.ID)
	let width = canvas.width
	//需要增益线
	if (gainColor != null)
	{
		drawGain(canvas, gain, gainColor, context)
		context.stroke()
		context.beginPath()
		// context.draw()
 	}
	//是否需要画满一屏
	if (canvas.totalCount < width)
	{
		// 用于画线的点
		let left = waveData.slice(0,Math.min(width - canvas.totalCount,waveData.length))
		drawLine(canvas,left,color,context)
		//如果已经画满，则需要丢弃
		if (waveData.length > left.length)
		{
			//剩余丢弃的点
			let right = waveData.slice(left.length)
			dropPoints(canvas, right.length, color, context)
		}
		context.stroke()
		context.draw()
		return
	}
	//是否继续丢弃
	else if (canvas.dropCount < lineSpace)
	{
		let drop = waveData.slice(0,Math.min(lineSpace - canvas.dropCount,waveData.length))
		dropPoints(canvas, drop.length, color, context)
		//丢弃的点已足够，剩余的点绘制
		if (waveData.length > drop.length)
		{
			let draw = waveData.slice(drop.length)
			makeShape(canvas, draw, color, context)
		}
		context.stroke()
		context.draw()
		return
	}
	else //不用画满也不用丢弃点的时候，一直做形变
	{
		let left = waveData.slice(0, Math.min(width - lineSpace - canvas.circleCount,waveData.length))
		makeShape(canvas, left, color, context)
		if (waveData.length > left.length)
		{
			canvas.dropCount = 0;
			canvas.circleCount = 0;
			canvas.totalCount = width - lineSpace;
			//用于画满一屏
			let right = waveData.slice(left.length)
			drawLine(canvas, right, color, context)
		}
		context.stroke()
		context.draw()
	}
}

function drawLine(canvas, waveData, color, context)
{
	let height = canvas.height
	context.setStrokeStyle(color)
	context.setLineWidth(1)
 	for (var i = 0; i < waveData.length ; i++)
	{
		canvas.drawPoints[canvas.drawPoints.length] = waveData[i]
	}
	context.moveTo(0, canvas.drawPoints[0])
	for (var i = 0; i < canvas.drawPoints.length; i++)
	{
		context.lineTo(i, canvas.drawPoints[i])
	}
	canvas.totalCount += waveData.length
}

function dropPoints(canvas, dropCount,color,context)
{
	let height = canvas.height
	for (var i = 0; i < dropCount; i++)
	{
		canvas.drawPoints.splice(0,1)
	}
	canvas.dropCount += dropCount
 	context.setStrokeStyle(color)
	context.setLineWidth(1)
 	context.moveTo(canvas.dropCount, canvas.drawPoints[0] )
	for (var i = 1; i < canvas.drawPoints.length; i++)
	{
		context.lineTo(canvas.dropCount + i, canvas.drawPoints[i])
	}
}

function makeShape(canvas,waveData,color,context)
{
	let height = canvas.height
   //用新的点置换旧点
	for (var i = 0; i < waveData.length;i++)
	{
		canvas.drawPoints[canvas.drawPoints.length] = waveData[i]
		canvas.drawPoints.splice(0,1)
	}
	canvas.circleCount += waveData.length;

	context.setStrokeStyle(color)
	context.setLineWidth(1)

	//绘制右边
	context.moveTo(canvas.circleCount + lineSpace, canvas.drawPoints[0])
	for (var i = 1; i < canvas.drawPoints.length - canvas.circleCount; i++)
	{
		context.lineTo(i + canvas.circleCount + lineSpace, canvas.drawPoints[i])
	}
	//绘制左边

	context.moveTo(0,canvas.drawPoints[canvas.drawPoints.length - canvas.circleCount])
	for (var i = 1; i < canvas.circleCount; i++)
	{
		context.lineTo(i, canvas.drawPoints[canvas.drawPoints.length - canvas.circleCount + i])
	}
}

function drawGain(canvas,gain,color,context)
{
	context.setStrokeStyle(color)
	context.setLineWidth(1)
	let realGain = (gain == 0)?0.5:gain
	if (gain > 6) realGain = 6
	var gridHeight = canvas.height * 1.0 / 6
	let space = 10;
	let left = 2
	//横
	context.moveTo(left, canvas.height * 0.5 - realGain * 0.5 * gridHeight)
	context.lineTo(space + left, canvas.height * 0.5 - realGain * 0.5 * gridHeight)
	//竖
	context.moveTo(space * 0.5 + left, canvas.height * 0.5 - realGain * 0.5 * gridHeight)
	context.lineTo(space * 0.5 + left, canvas.height * 0.5 + realGain * 0.5 * gridHeight)
	//横
	context.moveTo(left, canvas.height * 0.5 + realGain * 0.5 * gridHeight)
	context.lineTo(space + left, canvas.height * 0.5 + realGain * 0.5 * gridHeight)


}

function clean(canvas)
{
	var context = wx.createCanvasContext(canvas.ID)
	context.clearRect(0, 0, canvas.width, canvas.height)
	context.stroke()
	context.draw()
	canvas.drawPoints = []
	canvas.dropCount = 0
	canvas.circleCount = 0
	canvas.totalCount = 0
}

function getResultString(result)
{
	switch (result)
	{
		case 0:
			return "脉搏节律未见异常。"
		case 1:
			return "疑似脉率稍快。"
		case 2:
			return "疑似脉率过快。"
		case 3:
			return "疑似阵发性脉率过快。"
		case 4:
			return "疑似脉率稍缓。"
		case 5:
			return "疑似脉率过缓。"
		case 6:
			return "疑似偶发脉搏间期缩短。"
		case 7:
			return "疑似脉搏间期不规则。"
		case 8:
			return "疑似脉率稍快伴有偶发脉搏间期缩短。"
		case 9:
			return "疑似脉率稍缓伴有偶发脉搏间期缩短。"
		case 0x0A:
			return "疑似脉率稍快伴有脉搏间期不规则。"
		case 0xFF:
			return "信号质量差，请重新测量。"
		default :
			return "手指脱落。"
	}
}

function setResult(canvas,result)
{
	// canvas.drawPoints = []
	canvas.drawPoints = []
	canvas.dropCount = 0
	canvas.circleCount = 0
	canvas.totalCount = 0

	var ctx = wx.createCanvasContext(canvas.ID)
	let resultStr = getResultString(result)
	ctx.setFontSize(canvas.width * 1.0 / 18.5)
	ctx.setTextAlign('center')
	ctx.setTextBaseline('middle')
	ctx.fillText(resultStr, canvas.width * 0.5, canvas.height * 0.5)
	ctx.draw()
	return resultStr
}

//获取心电绘制的图片
function getEcgDrawImage(canvas, drawPoints, pixelRatio,ecgColor,gain,gainColor,successCallBack, failCallBack)
{
	if (successCallBack == null) return
	let height = canvas.height
	var context = wx.createCanvasContext(canvas.ID)
	//需要增益线
	if (gainColor != null)
	{
		drawGain(canvas, gain, gainColor, context)
		context.stroke()
		context.beginPath()
	}
	context.setStrokeStyle(ecgColor)
	context.setLineWidth(1)
	context.moveTo(0, drawPoints[0])
	let x_space = canvas.width * 1.0 / drawPoints.length
	for (var i = 1; i < drawPoints.length; i++)
	{
		context.lineTo(i * x_space,drawPoints[i])
	}
	context.stroke()
	context.draw(false,function(e){
		wx.canvasToTempFilePath({
			x: 0,
			y: 0,
			width: canvas.width,//
			height: canvas.height ,
			destWidth: canvas.width * pixelRatio,
			destHeight: canvas.height * pixelRatio,
			canvasId: canvas.ID,
			fileType: "png",
			success(res) {
				successCallBack(res)
			},
			fail(res) {
				if (failCallBack != null)
					failCallBack(res)
			},
			complete(res)
			{
				console.log(res)
			}
		})
	})
}

module.exports.drawWave = drawWave

module.exports.addPoints = addPoints
module.exports.addPointsWithGainline = addPointsWithGainline

module.exports.clean = clean
module.exports.initialWaveView = initialWaveView
module.exports.setResult = setResult
module.exports.drawGain = drawGain
module.exports.getEcgDrawImage = getEcgDrawImage


